package com.ruoyi.project.module.lyq;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.controller.YqgBarBillsController;
import com.ruoyi.project.domain.*;
import com.ruoyi.project.service.*;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * 消费详细Controller
 * 
 * @author dr
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/lyq/details")
public class LyqYqgConsumptionDetailsController extends BaseController
{
    @Autowired
    private IYqgConsumptionDetailsService yqgConsumptionDetailsService;
    @Autowired
    private IYqgHandsService yqgHandsService;
    @Autowired
    private IYqgBaseinfoService yqgBaseinfoService;
    @Autowired
    private IYqgBarBillsService yqgBarBillsService;
    @Autowired
    private IYqgInformationOrderingService yqgInformationOrderingService;
    /**
     * 查询消费详细列表
     */
//    @RequiresPermissions("project:details:list")
    @GetMapping("/list")
    public TableDataInfo list(YqgConsumptionDetails yqgConsumptionDetails)
    {
        startPage();
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        for(YqgConsumptionDetails q :list ){
//            System.out.println(q.getHandBrand()+"466");
        }
        return getDataTable(list);
    }

    /**
     * 导出消费详细列表
     */
//    @RequiresPermissions("project:details:export")
    @Log(title = "消费详细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgConsumptionDetails yqgConsumptionDetails)
    {
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        ExcelUtil<YqgConsumptionDetails> util = new ExcelUtil<YqgConsumptionDetails>(YqgConsumptionDetails.class);
        util.exportExcel(response, list, "消费详细数据");
    }

    /**
     * 获取消费详细详细信息
     */
//    @RequiresPermissions("project:details:query")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(yqgConsumptionDetailsService.selectYqgConsumptionDetailsByID(ID));
    }

    /**
     * 新增消费详细
     */
//    @RequiresPermissions("project:details:add")
    @Log(title = "消费详细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
    {
        //获取手牌号信息
            YqgHands yqgHands = new YqgHands();
            yqgHands.setHandBrand(yqgConsumptionDetails.getHandBrand());
            List<YqgHands> listhands = yqgHandsService.selectYqgHandsList(yqgHands);
            for (YqgHands hands :listhands )
            {
//                yqgConsumptionDetails.setHandBrand(hands.getHandBrand());
                yqgConsumptionDetails.setHandCategory(hands.getHandCategory());
                yqgConsumptionDetails.setHandState(hands.getHandState());
            }
        // 获取吧台账单信息
        YqgBarBills yqgBarBills=new YqgBarBills();
            yqgBarBills.setHandBrand(yqgConsumptionDetails.getHandBrand());
            yqgBarBills.setHandState(yqgConsumptionDetails.getHandState());
//            yqgBarBills.setHandCategory(yqgConsumptionDetails.getHandCategory());
        List<YqgBarBills> listbarbill = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
        for (YqgBarBills barbill :listbarbill )
        {
            yqgConsumptionDetails.setTogether(barbill.getTogether());
            yqgConsumptionDetails.setSailingsSigns(barbill.getSailings());
            yqgConsumptionDetails.setBarBillsId(barbill.getID());
            yqgConsumptionDetails.setRetentionStatus(barbill.getRetentionStatus());
            yqgConsumptionDetails.setMergeStatus(barbill.getMergeStatus());
            yqgConsumptionDetails.setOpeningTime(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",barbill.getOpeningTime()));

        }
        // 获取当前登录用户信息
        LoginUser loginUser = getLoginUser();
        YqgBaseinfo yqgBaseinfo =new YqgBaseinfo();
        yqgBaseinfo.setUserName(loginUser.getUsername());
        System.out.println(yqgBaseinfo.getUserName());
        List<YqgBaseinfo> listbaseinfo = yqgBaseinfoService.selectYqgBaseinfoList(yqgBaseinfo);
        for (YqgBaseinfo baseinfos :listbaseinfo )
        {
            System.out.println(yqgBaseinfo.getUserName());
            yqgConsumptionDetails.setUserId(baseinfos.getUserId());
            yqgConsumptionDetails.setDeptId(baseinfos.getDeptId());
            yqgConsumptionDetails.setDepartment(baseinfos.getDepartment());
            yqgConsumptionDetails.setNickName(baseinfos.getNickName());
            yqgConsumptionDetails.setUserName(baseinfos.getUserName());
            yqgConsumptionDetails.setPost(baseinfos.getPost());
        }
        // 获取点餐信息
        YqgInformationOrdering yqgInformationOrdering =new YqgInformationOrdering();
        yqgInformationOrdering.setOrderNumber(yqgConsumptionDetails.getOrderNumber());
        List<YqgInformationOrdering> listorder = yqgInformationOrderingService.selectYqgInformationOrderingList(yqgInformationOrdering);
        for (YqgInformationOrdering order :listorder )
        {
            yqgConsumptionDetails.setSeat(order.getTableNumber());
        }

        return toAjax(yqgConsumptionDetailsService.insertYqgConsumptionDetails(yqgConsumptionDetails));
    }

    /**
     * 修改消费详细
     */
//    @RequiresPermissions("project:details:edit")
    @Log(title = "消费详细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
    {
        return toAjax(yqgConsumptionDetailsService.updateYqgConsumptionDetails(yqgConsumptionDetails));
    }

    /**
     * 删除消费详细
     */
//    @RequiresPermissions("project:details:remove")
    @Log(title = "消费详细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        return toAjax(yqgConsumptionDetailsService.deleteYqgConsumptionDetailsByIDs(IDs));
    }
}
